CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0 FATAL_ERROR)
cmake_policy(SET CMP0012 NEW)
include(TestBigEndian)
find_package(PythonInterp 3.6)
project(graphite2)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})
if (NOT CMAKE_BUILD_TYPE)
    set_property(CACHE CMAKE_BUILD_TYPE PROPERTY VALUE Release)
endif()

option(BUILD_SHARED_LIBS "Make library a shared library instead of static" ON)

enable_language(CXX C)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED 11)

include_directories(${PROJECT_SOURCE_DIR}/include)

enable_testing()

set(GRAPHITE2_VM_TYPE auto CACHE STRING "Choose the type of vm machine: Auto, Direct or Call.")
option(GRAPHITE2_NFILEFACE "Compile out the gr_make_file_face* APIs")
option(GRAPHITE2_NTRACING "Compile out log segment tracing capability" ON)
option(GRAPHITE2_TELEMETRY "Add memory usage telemetry")
set(GRAPHITE2_SANITIZERS "" CACHE STRING "Set compiler sanitizers passed to -fsanitize")
set(GRAPHITE2_FUZZING_ENGINE libFuzzer.a CACHE STRING "Fuzzing engine to link against for the fuzzers")

message(STATUS "Build: " ${CMAKE_BUILD_TYPE})
string(REPLACE "ON"  "shared"  _LIB_OBJECT_TYPE ${BUILD_SHARED_LIBS})
string(REPLACE "OFF" "static"  _LIB_OBJECT_TYPE ${_LIB_OBJECT_TYPE})
string(REPLACE "ON" "disabled" _FILEFACE_SUPPORT ${GRAPHITE2_NFILEFACE})
string(REPLACE "OFF" "enabled" _FILEFACE_SUPPORT ${_FILEFACE_SUPPORT})
string(REPLACE "ON" "disabled" _TRACING_SUPPORT ${GRAPHITE2_NTRACING})
string(REPLACE "OFF" "enabled" _TRACING_SUPPORT ${_TRACING_SUPPORT})
message(STATUS "Building library: " ${_LIB_OBJECT_TYPE})
message(STATUS "File Face support: " ${_FILEFACE_SUPPORT})
message(STATUS "Tracing support: " ${_TRACING_SUPPORT})

if (GRAPHITE2_SANITIZERS)
    string(STRIP ${GRAPHITE2_SANITIZERS} GRAPHITE2_SANITIZERS)
    add_compile_options(-fsanitize=${GRAPHITE2_SANITIZERS} -fno-omit-frame-pointer -g -O1)
    message(STATUS "Sanitizer support: " ${GRAPHITE2_SANITIZERS})
    string(REGEX REPLACE "(,?fuzzer|fuzzer,?)" "" SANITIZERS_LINK  ${GRAPHITE2_SANITIZERS})
    if (GRAPHITE2_SANITIZERS MATCHES "fuzzer")
      set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize-coverage=trace-pc-guard")
    endif ()
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=${SANITIZERS_LINK}")
endif ()

string(TOLOWER ${GRAPHITE2_VM_TYPE} GRAPHITE2_VM_TYPE)
if (NOT GRAPHITE2_VM_TYPE MATCHES "auto|direct|call")
    message(SEND_ERROR "unrecognised vm machine type: ${GRAPHITE2_VM_TYPE}. Only Auto, Direct or Call are available")
endif()
if (GRAPHITE2_VM_TYPE STREQUAL "auto")
    if (CMAKE_BUILD_TYPE MATCHES "[Rr]el(ease|[Ww]ith[Dd]eb[Ii]nfo)")
        set(GRAPHITE2_VM_TYPE "direct")
    else(CMAKE_BUILD_TYPE MATCHES "[Rr]el(ease|[Ww]ith[Dd]eb[Ii]nfo)")
        set(GRAPHITE2_VM_TYPE "call")
    endif()
endif()
if (GRAPHITE2_VM_TYPE STREQUAL "direct" AND NOT (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
    message(WARNING "vm machine type direct can only be built using GCC")
    set(GRAPHITE2_VM_TYPE "call")
endif()
message(STATUS "Using vm machine type: ${GRAPHITE2_VM_TYPE}")

if (BUILD_SHARED_LIBS)
    if (PYTHONINTERP_FOUND)
        EXEC_PROGRAM(${PYTHON_EXECUTABLE}
            ARGS -c \"import ctypes\; print(ctypes.sizeof(ctypes.c_void_p)) \"
            OUTPUT_VARIABLE PYTHON_SIZEOF_VOID_P)
        if (NOT (PYTHON_VERSION_MAJOR GREATER 2 AND PYTHON_VERSION_MINOR GREATER 5))
            message(WARNING "Python version 3.6 or greater required. Disabling some tests.")
        elseif ((PYTHON_SIZEOF_VOID_P STREQUAL CMAKE_SIZEOF_VOID_P) AND (NOT GRAPHITE2_NFILEFACE) AND ((NOT GRAPHITE2_SANITIZERS MATCHES "address") OR CMAKE_COMPILER_IS_GNUCXX))
            set(PYTHON_CTYPES_COMPATBILE TRUE)
        else ()
            if ((NOT GRAPHITE2_SANITIZERS MATCHES "address") OR CMAKE_COMPILER_IS_GNUCXX)
                message(WARNING "Python ctypes is incompatible with built DLL. Disabling some tests.")
            endif ()
        endif ()
    endif()
endif()

add_subdirectory(src)
add_subdirectory(tests)
add_subdirectory(doc)
if (NOT GRAPHITE2_NFILEFACE)
    add_subdirectory(gr2fonttest)
endif()

set(version 3.0.1)
set(libdir ${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX})
set(includedir ${CMAKE_INSTALL_PREFIX}/include)

configure_file(graphite2.pc.in graphite2.pc)

install(FILES ${PROJECT_BINARY_DIR}/graphite2.pc DESTINATION lib${LIB_SUFFIX}/pkgconfig)
